﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using CamHelper.Interpreter;

namespace CamHelper.ProcessEditor
{
    public partial class OperationDefaultValuesControl : UserControl
    {
        private List<string> _expandetCategories;

        public OperationDefaultValuesControl()
        {
            _expandetCategories = new List<string>();
            InitializeComponent();
        }

        private void FillTree()
        {
            treeView1.SuspendLayout();
            treeView1.Nodes.Clear();
            var selected = cbbCategorie.Text;
            cbbCategorie.Items.Clear();
            _expandetCategories.Clear();

            var config = ((MainForm)ParentForm).Config;
            var pc = config.Parameter.Search((selected == "All" ? String.Empty : selected), txbSearch.Text, cbxOnlyUserPar.Checked);

            cbbCategorie.Items.Add("All");
            foreach (var item in pc.Categories)
            {
                cbbCategorie.Items.Add(item);
            }
            cbbCategorie.Text = (selected.Length > 0 ? selected : "All");

            if (cbxExpandNodes.Checked)
            {

                foreach (var item in pc)
                {
                    AddNode(item.Value);
                }
                treeView1.ExpandAll();
            }
            else
            {
                foreach (var item in pc.Categories)
                {
                    var cn = CreateCategorieNode(item);
                }
                treeView1.CollapseAll();
            }
            treeView1.ResumeLayout();
        }

        private void AddNode(IParameter p)
        {
            TreeNode parent;

            if (!treeView1.Nodes.ContainsKey(p.Category))
            {
                parent = CreateCategorieNode(p.Category);
                parent.Nodes.Clear();
            }
            else
                parent = treeView1.Nodes[p.Category];

            var node = new TreeNode();
            node.Tag = p;
            node.Text = p.Key;
            node.ToolTipText = p.ToString();
            if (!cbxOnlyUserPar.Checked && p.BaseEntry != null)
                node.NodeFont = new Font(treeView1.Font, System.Drawing.FontStyle.Bold);
            parent.Nodes.Add(node);
        }

        private TreeNode CreateCategorieNode(string categorie)
        {
            TreeNode parent;
            parent = new TreeNode();
            parent.Text = categorie;
            parent.Name = categorie;
            parent.Nodes.Add("Empty");
            treeView1.Nodes.Add(parent);
            return parent;
        }

        private void FillCategorieNodes(string categorie)
        {
            var config = ((MainForm)ParentForm).Config;
            var pc = config.Parameter.Search(categorie, txbSearch.Text, cbxOnlyUserPar.Checked);

            var parent = treeView1.Nodes[categorie];
            parent.Nodes.Clear();

            foreach (var item in pc)
            {
                AddNode(item.Value);
            }

            _expandetCategories.Add(categorie);
        }

        private void OperationDefaultValuesControl_Load(object sender, System.EventArgs e)
        {
            if (!DesignMode)
            {
                FillTree();
            }
        }

        private void cbxExpandNodes_CheckedChanged(object sender, System.EventArgs e)
        {
            FillTree();
        }

        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            FillTree();
        }

        private void cbxOnlyUserPar_CheckedChanged(object sender, System.EventArgs e)
        {
            FillTree();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var p = e.Node.Tag as IParameter;
            if (p != null)
            {
                txbKey.Text = p.Key;
                txbValue.Text = p.Value;
                if (p.BaseEntry != null)
                    txbBaseValue.Text = p.BaseEntry.Value;
                else
                    txbBaseValue.Text = String.Empty;
            }
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes[0].Text == "Empty")
            {
                e.Node.Nodes.Clear();
            }

            if (!cbxExpandNodes.Checked && !_expandetCategories.Contains(e.Node.Name))
            {
                FillCategorieNodes(e.Node.Text);
            }
        }
    }
}
